﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using Ecopsy.DataTier;
using SqlLibrary;

public partial class iFarmeAdmin_SiteMap : System.Web.UI.Page  
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) MyDataBind();
    }
    protected void AddButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("iFarmeAdmin_EditSiteMap.aspx?SiteMapItemID="+ Guid.NewGuid ().ToString ()+"&AgencyID="+AgencyID );
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        foreach (ListViewDataItem item in lvDiscounts.Items)
        {
            CheckBoxList chList = (CheckBoxList)item.FindControl("chRoleList");
            HiddenField hfID = (HiddenField)item.FindControl("hfID");
            UpadetMapItem(chList, hfID);
            
        }
        wsAgency ws = new wsAgency();            
        ws.SaveAgency ();
    }

    private void UpadetMapItem(CheckBoxList chList, HiddenField hfID)
    {
        
        SiteMapItem item = (SiteMapItem)Agency.SitMap[hfID.Value];
        item.Roles.Clear();
        foreach (ListItem i in chList.Items)
        {
            if (i.Selected) item.Roles.Add(new EntitySingle() { ID = i.Value });
        }        

    }


    protected string GetCommnadFunction(string ID)
    {        
        string sUrl="iFarmeAdmin_EditSiteMap.aspx?SiteMapItemID=" + ID+"&AgencyID="+AgencyID;
        return "Redirect('" + sUrl + "');";
    }
  
    protected string GetRoles(string ID)
    {
        return "Роли";
    }
   
    protected void OnDeleteClick(object sender, EventArgs e)
    {
        //ImageButton btn = (ImageButton)sender;
        //GlobalConstants.CurrentAgency.SurfaceGroups.Remove(btn.CommandArgument);

        //wsAgency ws = new wsAgency();
        //ws.SaveAgency();
        //MyDataBind();

    }

    public SiteMapItem [] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {
        Agency a = Agency;
        
        SiteMapItem [] r= a.SitMap.ToArray<SiteMapItem>();    
        //заряжаем comparer функцией сортировки 
        ListWhere w = new ListWhere();
        w.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        // Сортируем        
        Array.Sort(r, w.SortCondition);
        RowCount = int.MaxValue;
        return r;
    }

    protected string AgencyID { get { return Request.QueryString["AgencyID"]; } }
   
    private void MyDataBind()
    {
        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvDiscounts.DataSource = ODS.ImAsObjectDataSource;
        DataBind();

    }
   
    public ListItemCollection GetChSource(string ID)
         
    {
        Agency a = Agency ;
        SiteMapItem item = (SiteMapItem)a.SitMap[ID];        
        return GetEmplRoles(item);
    }

    private ListItemCollection GetEmplRoles(SiteMapItem SiteMapItem)
    {
        ListItemCollection res = new ListItemCollection();

        ////список ролей
        ws_Securety ws = new ws_Securety();
        RoleList rl = ws.GetRoleList();
        foreach (DictionaryItem item in rl)
        {
            ListItem i = new ListItem();
            i.Value = item.ID;
            i.Text = item.Value.ToString();
            i.Selected = (null != SiteMapItem.Roles[item.ID]);
            res.Add(i);
        }
        return res;
    }

    protected void lvDiscounts_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        SiteMapItem item = (SiteMapItem)(((System.Web.UI.WebControls.ListViewDataItem)(e.Item)).DataItem);
        CheckBoxList chList = (CheckBoxList)e.Item.FindControl("chRoleList");

        ListItemCollection coll = GetEmplRoles(item);
        foreach (ListItem itm in coll)
        { 
            ListItem l = new ListItem ();
            l.Value = itm.Value;
            l.Text = itm.Text;
            l.Selected = itm.Selected;
            chList.Items .Add (l); 
        }

    }

    protected Agency Agency { get { wsAgency ws = new wsAgency(); return ws.GetAgency(AgencyID); } }
    protected void btnDelClick(object sender, ImageClickEventArgs e)
    {
        ImageButton btn = (ImageButton)sender;
        string s = btn.CommandArgument;
        Agency.SitMap.Remove(s);
        wsAgency ws = new wsAgency();
        ws.SaveAgency(Agency);
        MyDataBind();

    }
}
